package com.shuorikeji.huohuo.app;

import android.app.Activity;
import android.content.Context;

import com.shuorikeji.huohuo.utils.PreUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;


public class AppManager {
    private static Stack<Activity> activityStack;
    private volatile static AppManager instance;
    private static Context mContext;

    private AppManager(Context mCon) {
        mContext = mCon;
    }

    //所有的activity列表
    private List<Activity> activities=new ArrayList<>();

    static void initialize(Context context) {
        if (instance == null) {
            synchronized (AppManager.class) {
                if (instance == null) {
                    instance = new AppManager(context.getApplicationContext());
                    //初始化缓存
                    PreUtils.init(mContext);
                }
            }
        }
    }

    /**
     * 单一实例
     */

    public static AppManager getAppManager() {
        if (instance == null) {
            synchronized (AppManager.class) {
                if (instance == null) {
                    instance = new AppManager(mContext.getApplicationContext());
                }
            }
        }
        return instance;
    }

    /**
     * 添加Activity到堆栈
     */
    public void addActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack<Activity>();
        }
        activityStack.add(activity);
    }

    /**
     * 获取当前Activity（堆栈中最后一个压入的）
     */
    public Activity currentActivity() {
        Activity activity = activityStack.lastElement();
        return activity;
    }

    /**
     * 结束当前Activity（堆栈中最后一个压入的）
     */
    public void finishActivity() {
        Activity activity = activityStack.lastElement();
        finishActivity(activity);
    }

    /**
     * 结束指定的Activity
     */
    public void finishActivity(Activity activity) {
        // 应用即将全部关闭，清理缓存
        // if(activityStack.size()==1){
        // ((AppContext)activity.getApplication()).clearWebViewCache();
        //
        // }
        if (activity != null) {
            activityStack.remove(activity);
            activity.finish();
            activity = null;
        }
    }

    /**
     * 结束指定类名的Activity
     */
    public synchronized void finishActivity(Class<?> cls) {
//		Iterator<Activity> iterator = activityStack.iterator();
//		while (iterator.hasNext()){
//			Activity activity = iterator.next();
//			if(activity.getClass().equals(cls)){
//				iterator.remove();
//				finishActivity(activity);
//			}
//		}
        for (Activity activity : activityStack) {
            if (activity.getClass().equals(cls)) {
                finishActivity(activity);
            }
        }
    }

    // 获取指定类名的Activity
    public Activity getActivity(Class<?> cls) {
        for (Activity activity : activityStack) {
            if (activity.getClass().equals(cls)) {
                return activity;
            }
        }
        return null;
    }

    /**
     * 结束所有Activity
     */
    public void finishAllActivity() {
        if (activityStack != null) {
            for (int i = 0, size = activityStack.size(); i < size; i++) {
                if (null != activityStack.get(i)) {
                    activityStack.get(i).finish();
                }
            }
            activityStack.clear();
        }
    }

    public synchronized void closeAllActivity() {
        synchronized (activities) {
            Iterator<Activity> iter = activities.iterator();
            while (iter.hasNext()) {
                Activity activity = iter.next();
                iter.remove();
                if (activity != null && !activity.isFinishing()) {
                    activity.finish();
                }
            }
        }
    }

    /**
     * 退出应用程序
     */
    public void AppExit(Context context) {
        try {
            finishAllActivity();
            System.exit(0);
        } catch (Exception e) {
        }
    }
}
